package com.example.demo.utils;

import com.example.demo.bean.bo.Prize;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by lyc on 2019/7/14.
 *
 * 根据概率将奖品划分区间，每个区间代表一个奖品，然后抽取随机数，反查落在那个区间上，即为所抽取的奖品。
 */
public class Arithmetic {
    // 放大倍数
    private static final int mulriple = 1000000;

    /**
     * 抽奖
     * @param prizes
     * @return
     */
    public int lottery(List<Prize> prizes) {
        Assert.isTrue(!CollectionUtils.isEmpty(prizes),"奖品信息不能为空");
        int lastScope = 0;
        // 洗牌，打乱奖品次序
        Collections.shuffle(prizes);
        Map<Integer, int[]> prizeScopes = new HashMap<Integer, int[]>();
        Map<Integer, Integer> prizeQuantity = new HashMap<Integer, Integer>();
        for (Prize prize : prizes) {
            int prizeId = prize.getPrizeId();
            // 划分区间
            int currentScope = lastScope + prize.getProbability().multiply(new BigDecimal(mulriple)).intValue();
            prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope });
            prizeQuantity.put(prizeId, prize.getQuantity());

            lastScope = currentScope;
        }

        // 获取1-1000000之间的一个随机数
        int luckyNumber = new Random().nextInt(mulriple);
        int luckyPrizeId = 0;
        // 查找随机数所在的区间
        if ((null != prizeScopes) && !prizeScopes.isEmpty()) {
            Set<Map.Entry<Integer, int[]>> entrySets = prizeScopes.entrySet();
            for (Map.Entry<Integer, int[]> m : entrySets) {
                int key = m.getKey();
                if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1] && prizeQuantity.get(key) > 0) {
                    luckyPrizeId = key;
                    break;
                }
            }
        }

        if (luckyPrizeId > 0) {
            // 奖品库存减一
        }
        return luckyPrizeId;
    }
    //通过1000次抽取
    public static void main(String[] args) {
        List<Prize> prizes = new ArrayList<Prize>();
        Prize prize1 = new Prize();
        prize1.setPrizeId(1);
        prize1.setProbability(new BigDecimal(0.05));
        prize1.setQuantity(1);
        prizes.add(prize1);

        prize1.setName("一等奖");

        Prize prize2 = new Prize();
        prize2.setPrizeId(2);
        prize2.setProbability(new BigDecimal(0.10));
        prize2.setQuantity(10);
        prize2.setName("二等奖");
        prizes.add(prize2);

        Prize prize3 = new Prize();
        prize3.setPrizeId(3);
        prize3.setProbability(new BigDecimal(0.15));
        prize3.setQuantity(20);
        prizes.add(prize3);
        prize3.setName("三等奖");

        Prize prize4 = new Prize();
        prize4.setPrizeId(4);
        prize4.setProbability(new BigDecimal(0.20));
        prize4.setQuantity(50);
        prizes.add(prize4);
        prize4.setName("四等奖");

        Prize prize5 = new Prize();
        prize5.setPrizeId(5);
        prize5.setProbability(new BigDecimal(0.50));
        prize5.setQuantity(200);
        prize5.setName("谢谢惠顾");
        prizes.add(prize5);

        int prize1GetTimes = 0;
        int prize2GetTimes = 0;
        int prize3GetTimes = 0;
        int prize4GetTimes = 0;
        int prize5GetTimes = 0;
        Arithmetic arithmetic = new Arithmetic();
        int times = 1000;
        for (int i = 0; i < times; i++) {
            int prizeId = arithmetic.lottery(prizes);
            switch (prizeId) {
                case 1:
                    prize1GetTimes++;
                    break;
                case 2:
                    prize2GetTimes++;
                    break;
                case 3:
                    prize3GetTimes++;
                    break;
                case 4:
                    prize4GetTimes++;
                    break;
                case 5:
                    prize5GetTimes++;
                    break;
            }
        }
        System.out.println("抽奖次数" + times);
        System.out.println("一等奖中奖次数" + prize1GetTimes);
        System.out.println("二等奖中奖次数" + prize2GetTimes);
        System.out.println("三等奖中奖次数" + prize3GetTimes);
        System.out.println("四等奖中奖次数" + prize4GetTimes);
        System.out.println("谢谢惠顾中奖次数" + prize5GetTimes);
    }
}
